package com.rzl.expense.quartz.task;


import com.rzl.expense.agent.DataAgentService;
import com.rzl.expense.common.utils.DateUtils;
import com.rzl.expense.common.utils.StringUtils;
import com.rzl.expense.expense.domain.EleAmortizeInfo;
import com.rzl.expense.expense.domain.PaymentResourceDetail;
import com.rzl.expense.expense.domain.RentPayment;
import com.rzl.expense.expense.domain.RptPrvEleSitePayable;
import com.rzl.expense.expense.service.IEleAmortizeInfoService;
import com.rzl.expense.expense.service.IRptPrvEleSitePayableService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 电费应付月报
 *
 * @author admin
 * @date 2023/08/09
 */
@Slf4j
@Component("RptPrvEleSitePayableTask")
public class RptPrvEleSitePayableTask {
    @Autowired
    private DataAgentService dataAgentService;

    @Autowired
    private IRptPrvEleSitePayableService rptPrvEleSitePayableService;


    public void run(String startMonth, String endMonth) {
        log.info("获取集团用户数据..........");
        // 执行sql
        List<String> tabDate = DateUtils.getMonths(startMonth, endMonth);

        for (String date : tabDate) {


            int size;
            // limit起始位置
            int startIndex = 0;
            // limit大小
            int limit = 50000;
            String[] onYear = date.split("-");
            Long year = Long.valueOf(onYear[0]);
            Long month = Long.valueOf(onYear[1]);
            rptPrvEleSitePayableService.deleteRptPrvEleSitePayableByDate(year, month);
            do {
                List<RptPrvEleSitePayable> list = (List<RptPrvEleSitePayable>) dataAgentService.getData(RuleSqlAdapter.getRptPrvEleSitePayable(year, month, startIndex, limit), RptPrvEleSitePayable.class);
                log.info("获取集团用户数据：{}条", list.size());
                if (!Objects.isNull(list) && list.size() > 0) {
                    int insertSize = list.size();
                    // limit起始位置
                    int insertIndex = 0;

                    // limit大小
                    do {
                        List<RptPrvEleSitePayable> rptPrvEleSitePayables = list.stream().skip(insertIndex).limit(2000).collect(Collectors.toList());
                        try {
                            if (!Objects.isNull(rptPrvEleSitePayables) && rptPrvEleSitePayables.size() > 0) {
                                rptPrvEleSitePayableService.batchRptPrvEleSitePayable(rptPrvEleSitePayables);
                                insertSize = rptPrvEleSitePayables.size();
                                insertIndex = insertIndex + 2000;
                            } else {
                                insertSize = 0;
                            }
                        } catch (Exception e) {
                            e.printStackTrace();

                        }
                    } while (insertSize >= 2000);
                }

                size = list.size();
                startIndex = startIndex + limit;
            } while (size >= limit);

        }
    }
}
